package cn.com.cifi.mars.web.action.report;

import cn.com.cifi.mars.bean.bo.test.TestSalesData;
import cn.com.cifi.mars.bean.bo.test.TestSignVersionData;
import cn.com.cifi.mars.service.DmOdsMyVsMarsSContractService;
import cn.com.cifi.mars.service.TestDataReportService;
import cn.com.cifi.mars.service.VsMarsFeeService;
import cn.com.cifi.mars.service.VsMarsGetinService;
import cn.com.cifi.mars.util.DmAnalyticalTemplate;
import cn.com.cifi.mars.util.TestSalesDataTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author: JingYuanXiang
 * @date: 2020/12/14 17:33
 */
@Slf4j
@RestController
public class TestDataReportController implements ITestDataReportController {

    @Autowired
    private TestDataReportService testDataReportService;

    @Override
    public void testSalesDataExport(String apiVersion, HttpServletResponse response, String projectId) {
        TestSalesData data = testDataReportService.getTestSalesData(projectId);

        TestSalesDataTemplate template = new TestSalesDataTemplate();
        template.appendRowsOne(data.getTestSignRecordsList(),0);
        template.appendRowsTwo(data.getTestSignReceivableList(),1);
        template.appendRowsThree(data.getTestSignReceiptsList(),2);

        try (OutputStream outputStream = response.getOutputStream()){
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("测试签约数据".concat(".xlsx"), "utf-8"));
            template.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("###### errorMsg: ", e);
        }
    }

    @Override
    public void signVersionMoneyDataExport(String apiVersion, HttpServletResponse response, String signVersionId) {
        List<TestSignVersionData> dataList = testDataReportService.getTestSignVersionData(signVersionId);
        List<String> timeList = testDataReportService.getTestSignTimeList(signVersionId);

        TestSalesDataTemplate template = new TestSalesDataTemplate();
        template.appendRowsFour(dataList,timeList,3);

        try (OutputStream outputStream = response.getOutputStream()){
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("测试签约数据".concat(".xlsx"), "utf-8"));
            template.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("###### errorMsg: ", e);
        }
    }
}
